//
//  ProjectManageController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/5/24.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import MJRefresh
import RxSwift
import RxCocoa

class MyOrderController: CBBaseViewController {

    var tbOrder: UITableView!
    var userModel:UserModel?
    
    var arrOrder:[MyOrderModel] = [MyOrderModel]()
    let CELL_ID = "MY_ORDER_CELL_ID"
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我的认筹"
        
        tbOrder = UITableView(frame: .zero, style: .plain)
        tbOrder.rowHeight = 140
        tbOrder.separatorStyle = .none
        tbOrder.dataSource = self
        tbOrder.delegate = self
        tbOrder.tableFooterView = UIView()
        tbOrder.register(UINib(nibName: "MyOrderCell", bundle: nil), forCellReuseIdentifier: CELL_ID)
        tbOrder.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            self?.curPage = 1
            self?.initData()
        })
        tbOrder.mj_footer = MJRefreshFooter(refreshingBlock: {  [unowned self] in
            self.curPage += 1
            self.initData()
        })
        tbOrder.mj_footer.isHidden = true
        self.view.addSubview(tbOrder)
        
        tbOrder.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        tbOrder.mj_header.beginRefreshing()
    }
    
    var curPage:Int = 1
    let pageSize:Int = 20
    func initData() {
        NetworkTool.requestData(url: ActionURL.GET_ORDER_LIST, param: [
            "uid":MainTools.shared.userModel?.uid ?? "",
            "page":curPage,
            "perPage":pageSize])
            .map { (json) -> [MyOrderModel]? in
                return MyOrderModel.mj_objectArray(withKeyValuesArray: json["rows"].arrayObject) as? [MyOrderModel]
            }.catchErrorJustReturn(nil)
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self] tmpArr in
                self.tbOrder.mj_header.endRefreshing()
                self.tbOrder.mj_footer.endRefreshing()
                if let arrModel = tmpArr {
                    if self.curPage == 1 {
                        self.arrOrder = arrModel
                    } else {
                        self.arrOrder.append(contentsOf: arrModel)
                    }
                    self.tbOrder.mj_footer.isHidden = (arrModel.count < self.pageSize)
                    self.tbOrder.reloadData()
                }
            }).disposed(by: disposeBag)
    }
    
}

extension MyOrderController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrOrder.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MyOrderCell = (tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as? MyOrderCell) ?? MyOrderCell()
        cell.model = arrOrder[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let projectVc = MainTools.initSbVc(sbName: "Main", AnyClass: ProjectController.self)
        projectVc.param = [
            "product_id":arrOrder[indexPath.row].product_id
        ]
        self.navigationController?.pushViewController(projectVc, animated: true)
    }
    
}
